#!/usr/local/bin/python3
# *_* coding: UTF-8 *_*
# @IDE: PyCharm
# @Version: Python3.97
# @Author: Kendrick.Kc
# @Email: 509556985@qq.com
# @File: public.py
# @Inst: 公用函数
# @Time: 2023/5/13 22:42
# -----


from rest_framework.throttling import SimpleRateThrottle


class UserThrottling(SimpleRateThrottle):
    """
        节流
        API 访问次数的限制
        防止爬虫过度访问
        一般通过用户的ID或者IP当作唯一的keys
    """
    scope = "已认证用户"

    def get_cache_key(self, request, view):
        return request.user     # 用当前登陆的用户当作 keys
